package com.tourism.website.articlepersonal.controller;


import com.tourism.website.apis.ArticlePersonalApis;
import com.tourism.website.articlepersonal.entity.ArticleCategory;
import com.tourism.website.articlepersonal.service.IArticleCategoryService;
import com.tourism.website.articlepersonal.service.IArticleImgService;
import com.tourism.website.articlepersonal.service.IArticleService;
import com.tourismwebsite.articledisplay.dto.ArticleCategoryDTO;
import com.tourismwebsite.articlepersonal.dto.ArticleCategoryPersonalDTO;
import com.tourismwebsite.articlepersonal.dto.ArticlePersonalDTO;
import com.tourismwebsite.articlepersonal.query.ArticlePersonalQuery;
import com.tourismwebsite.vo.JsonVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kevin
 * @since 2024-06-13
 */
@RestController
@RequestMapping("/ArticlePersonal/")
@Api(tags = "普通用户文章管理")
public class ArticleController implements ArticlePersonalApis {
    @Resource
    IArticleService iArticleService;
    @Resource
    IArticleImgService iArticleImgService;
    @Resource
    IArticleCategoryService iArticleCategoryService;

    //查询用户所有文章
    @GetMapping("query-article-allPersonal")
    @Override
    @ApiOperation(value = "用户查询个人文章")
    public JsonVO<List<ArticlePersonalDTO>> queryAllArticlePersonal(String username) {
        List<ArticlePersonalDTO> result = iArticleService.queryArticlePersonal(username);
            return JsonVO.success(result);
    }

    //支持批量
    @DeleteMapping("delete-article-content")
    @Override
    @ApiOperation(value = "用户删除个人文章")
    public JsonVO<Boolean> deleteArticle(@RequestParam List<Integer> articleIds) {
        boolean allDeleted = true;
        for (Integer articleId : articleIds) {
            if (!iArticleService.deleteArticleManage(articleId)) {
                allDeleted = false;
                // 可以选择在这里进行日志记录或其他处理
            }
        }
        if (allDeleted) {
            return JsonVO.success(true);
        } else {
            return JsonVO.fail(null);
        }
    }

    //查询文章的名字和更新时间
    @PostMapping("query-article-nameAndTime")
    @Override
    @ApiOperation(value = "用户查询文章发布名称和时间")
    public JsonVO<List<ArticlePersonalDTO>> queryArticleNameAndTime(Integer articleId) {
        List<ArticlePersonalDTO> result = iArticleService.queryNameAndTime(articleId);
            return JsonVO.success(result);
    }

    //从种类（category）中查询到该分类(category)中的所有文章ID
    @PostMapping("query-article-category")
    @Override
    @ApiOperation(value = "用户根据种类查询文章")
    public JsonVO<List<ArticleCategoryPersonalDTO>> queryCategoryToArticleId(Integer categoryId) {
        List<ArticleCategoryPersonalDTO> result = iArticleCategoryService.queryCategoryToArticleID(categoryId);
            return JsonVO.success(result);
    }

    @PostMapping("save-article")
    @ApiOperation(value = "发布文章")
    @Override
    public JsonVO<Boolean> saveArticle(ArticlePersonalQuery articlePersonalQuery,Integer categoryId) {
        Boolean result = iArticleService.saveArticle(articlePersonalQuery,categoryId);
        boolean result1 = iArticleCategoryService.saveCategory(articlePersonalQuery.getArticleId(),categoryId);
        if (result&&result1) return JsonVO.create(null,10000,"成功添加");
        return JsonVO.create(result, 9999, "发布新文章失败");
    }
    @PostMapping("query_categoryByUserId")
    @Override
    @ApiOperation(value = "根据用户ID查找种类，获取文章")
    public JsonVO<List<ArticleCategoryDTO>> getArticlesByUserIdAndCategoryId(Integer userId, Integer categoryId) {
        List<ArticleCategoryDTO> result = iArticleService.getArticlesByUserIdAndCategoryId(userId,categoryId);
        return JsonVO.success(result);
    }
}


